import { HomeHttp } from '../../../api/home.js'
import { servicePointMap } from '../../../utils/dataMap.js'

const homeModel = new HomeHttp()
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    streetInfo: {}, // 街道信息
    recommendStoreList: [], // 附近门店
  },

  initPage(options) {
    if (options.isStreetInfo && options.isStreetInfo === 'true') {
      let streetInfo = wx.getStorageSync('street_info')
      console.log(streetInfo)
      this.setData({
        streetInfo: {
          name: streetInfo.street_id,
          province: streetInfo.province,
          city: streetInfo.city,
          area: streetInfo.area,
          popularity_tip: streetInfo.popularity_tip
        },
        recommendStoreList: servicePointMap(streetInfo.store_info)
      })
    } else {
      this.httpWxGetLocation()
    }
  },

  onSelectStore(event) {
    const { info } = event.currentTarget.dataset
    app.event.emit('changeServicePoint', info)
    // wx.setStorageSync('storeId', this.data.recommendStoreList[index].store_id)
    wx.switchTab({
      url: `/pages/home/index/index`
    })
  },

  // 获取附服务站
  httpGetRecommendStores(longitude = 0, latitude = 0) {
    return homeModel.getYddServicePoint({
      "lng": longitude, //经度
      "lat": latitude, //纬度
      "size": 0
    }).then((res) => {
      this.setData({
        streetInfo: res.service_point[0],
        recommendStoreList: servicePointMap(res.service_point)
      })
    })
  },

  // 获取地理位置信息
  httpWxGetLocation() {
    let self = this
    return new Promise((resolve, reject) => {
      wx.getLocation({
        type: 'wgs84',
        success: function(res) {
          self.httpGetRecommendStores(res.longitude, res.latitude)
          resolve(res)
        },
        fail: function(err) {
          reject(err)
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})